import moment from 'moment';

const base = '/alarm';

const nowEndDate = Date.parse(moment().format('YYYY-MM-DD 23:59:59'));
const before7Date = Date.parse(
    moment().subtract(6, 'day').format('YYYY-MM-DD 00:00:00')
);

const timer = [before7Date, nowEndDate];

/* --------------------- 告警信息 -------------------- */
// 告警信息接口 、根据传参不同来实现不同的功能
const queryAlarmInfo = {
    url: `/monitor/v1/oneQuery`,
    method: 'POST',
};
// 告警信息接口 、根据传参不同来实现不同的功能
const queryAlarmInfoExt = {
    url: `/monitor/v1/oneQuery/alarm`,
    method: 'POST',
};

// 告警 聚合接口
const queryAlarmInfoAgg = {
    url: `/monitor/v1/oneAgg`,
    method: 'POST',
};

// 大屏告警Echars获取数据接口
const queryScreenPageEchars = () => {
    // 将时间设为今天起3个月 避免查询数量过多
    // const timer = [moment().subtract(3, 'months').valueOf(),moment().valueOf()];
    const data = {
        collection: 'alarm_info_realtime',
        equalCondition: {
            alarmStatus: 1,
        },
        // "rangeCondition": {
        //     "alarmTime": timer,
        // },
        groupFields: ['regionCode', 'regionName', 'alarmLvl'],
        returnFields: {
            countValue: 0, // 总数
        },
    };
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

//  Echars 7天告警数据
const getEcharsData = (isPermission) => {
    // const timer = [before7Date,nowEndDate];
    let data = {
        collection: 'alarm_info_his',
        rangeCondition: {
            alarmTime: timer,
        },
        equalCondition: {
        },
        "notEqualCondition": {
            "alarmType": "capacity"
        },
        subQueryFields: {
            alarmDay: { existField: 1 },
            info_sum: {
                existField: 0,
                if: { alarmLvl: '5-info' },
                then: 1,
                else: 0,
            },
            emergency_sum: {
                existField: 0,
                if: { alarmLvl: '1-emergency' },
                then: 1,
                else: 0,
            },
            high_sum: {
                existField: 0,
                if: { alarmLvl: '2-high' },
                then: 1,
                else: 0,
            },
            mid_sum: {
                existField: 0,
                if: { alarmLvl: '3-mid' },
                then: 1,
                else: 0,
            },
            junior_sum: {
                existField: 0,
                if: { alarmLvl: '4-junior' },
                then: 1,
                else: 0,
            },
        },
        groupFields: ['alarmDay'],
        returnFields: {
            total_count: 0.0,
            info_sum: 0.0,
            emergency_sum: 0.0,
            high_sum: 0.0,
            mid_sum: 0.0,
            junior_sum: 0.0,
        },
        // "orderField":{"resourceName":"asc"},
    };
    if(isPermission) {
        data['permission'] = true;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 当前告警数量 groupBy resourceName
const getRealtimeAlarmGroupBy = (list) => {
    // 将时间设为今天起3个月 避免查询数量过多
    // const timer = [moment().subtract(3, 'months').valueOf(),moment().valueOf()];
    const data = {
        collection: 'alarm_info_realtime',
        equalCondition: { alarmStatus: 1 },
        // "rangeCondition": {
        //     "alarmTime": timer,
        // },
        "notEqualCondition": {
            "alarmType": "capacity"
        },
        groupFields: [...list],
        returnFields: {
            countValue: 0, // 总数
        },
    };
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 聚合接口 得到告警分类数据
const getRealtimeAlarmClass = () => {
    // 将时间设为今天起3个月 避免查询数量过多
    // const timer = [moment().subtract(3, 'months').valueOf(),moment().valueOf()];
    const data = {
        collection: 'alarm_info_realtime',
        equalCondition: {
            alarmStatus: 1,
        },
        "notEqualCondition": {
            "alarmType": "capacity"
        },
        subQueryFields: {
            resourceName: { existField: 1 },
            resourceCode: { existField: 1 },
            info_sum: {
                existField: 0,
                if: { alarmLvl: '5-info' },
                then: 1,
                else: 0,
            },
            emergency_sum: {
                existField: 0,
                if: { alarmLvl: '1-emergency' },
                then: 1,
                else: 0,
            },
            high_sum: {
                existField: 0,
                if: { alarmLvl: '2-high' },
                then: 1,
                else: 0,
            },
            mid_sum: {
                existField: 0,
                if: { alarmLvl: '3-mid' },
                then: 1,
                else: 0,
            },
            junior_sum: {
                existField: 0,
                if: { alarmLvl: '4-junior' },
                then: 1,
                else: 0,
            },
        },
        groupFields: ['resourceName', 'resourceCode'],
        returnFields: {
            total_count: 0.0,
            info_sum: 0.0,
            emergency_sum: 0.0,
            high_sum: 0.0,
            mid_sum: 0.0,
            junior_sum: 0.0,
        },
        // "orderField":{"resourceName":"asc"},
    };
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 查询近7天的告警历史数量
const queryNearly7DaysAlarm = ( isPermission, inCondition, equalCondition) => {
    // const timer = [before7Date,nowEndDate];
    let data = {
        collection: 'alarm_info_his',
        equalCondition: {
            ...equalCondition
        },
        "notEqualCondition": {
            "alarmType": "capacity"
        },
        rangeCondition: {
            alarmTime: timer, // 查历史表要用alarmTime
        },
        groupFields: [],
        returnFields: {
            countValue: 0, // 总数
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 查询近7天的告警历史 GroupBy
const getNearly7DaysAlarmGroupBy = (list, isPermission) => {
    // const timer = [moment().subtract(7, 'days').valueOf(),moment().valueOf()];
    let data = {
        collection: 'alarm_info_his',
        equalCondition: {
        },
        "notEqualCondition": {
            "alarmType": "capacity"
        },
        rangeCondition: {
            alarmTime: timer, // 查历史表要用alarmTime
        },
        groupFields: [...list],
        returnFields: {
            countValue: 0, // 总数
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 当前告警数量
const realtimeAlarmNum = (isRecovery, inCondition, equalCondition) => {
    const data = {
        collection: 'alarm_info_realtime',
        equalCondition: {
            alarmStatus: isRecovery,
            ...equalCondition,
        },
        inCondition,
        rangeCondition: {},
        groupFields: [],
        returnFields: {
            countValue: 0, // 总数
        },
    };
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 报警级别个数
const getAlarmLvlNum = (isRecovery) => {
    const data = {
        collection: 'alarm_info_realtime',
        equalCondition: {
            alarmStatus: isRecovery,
        },
        rangeCondition: {},
        groupFields: ['alarmLvl'],
        returnFields: {
            countValue: 0, // 总数
        },
    };
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// net天告警信息
const getNetEcharsData = (isPermission, inCondition, equalCondition) => {
    // const timer = [moment().subtract(7, 'days').valueOf(),moment().valueOf()];
    let data = {
        collection: 'net',
        equalCondition: {
            timeType: 1440,
            ...equalCondition
        },
        rangeCondition: {
            beginTime: timer,
        },
        groupFields: [
            "beginTime",
        ],
        returnFields: {
            averBytesRecvRate:0,
            averBytesSentRate:0,
            beginTimeStr: ''
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};
//首页网络流入流出速率
const getNetTop5Usage = (timerl = timer, isPermission, inCondition, equalCondition) => {
    let data = {
        collection: 'net',
        equalCondition: {
            timeType: 1440,
            ...equalCondition
        },
        notEqualCondition: {
            resourceUuid: '',
        },
        rangeCondition: {
            beginTime: timerl,
        },
        groupFields: ['resourceUuid'],
        orderField: {
            averBytesRecvRate: 'desc',
        },
        returnFields: {
            averBytesRecvRate:0,
            averBytesSentRate:0,
            resourceUuid: ''
        },
        limit: 5,
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};
// Cpu7天告警信息
const getCpuEcharsData = (isPermission, inCondition, equalCondition) => {
    // const timer = [moment().subtract(7, 'days').valueOf(),moment().valueOf()];
    let data = {
        collection: 'cpu',
        equalCondition: {
            timeType: 1440,
            ...equalCondition
        },
        rangeCondition: {
            beginTime: timer,
        },
        groupFields: [
            "beginTime",
        ],
        returnFields: {
            averUsage: 0, // 总数
            beginTimeStr: ''
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};
// mem7天告警信息
const getMemEcharsData = (isPermission, inCondition, equalCondition) => {
    // const timer = [moment().subtract(7, 'days').valueOf(),moment().valueOf()];
    let data = {
        collection: 'mem',
        equalCondition: {
            timeType: 1440,
            ...equalCondition
        },
        rangeCondition: {
            beginTime: timer,
        },
        groupFields: [
            "beginTime",
        ],
        returnFields: {
            averUsedPercent: 0, // 总数
            beginTimeStr: ''
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};
// disk 7天告警信息
const getDiskEcharsData = (isPermission, inCondition, equalCondition) => {
    // const timer = [moment().subtract(7, 'days').valueOf(),moment().valueOf()];
    let data = {
        collection: 'disk',
        equalCondition: {
            timeType: 1440,
            path: 'all',
            ...equalCondition,
        },
        rangeCondition: {
            beginTime: timer,
        },
        groupFields: [
            "beginTime",
        ],
        returnFields: {
            averUsedPercent: 0, // 总数
            beginTimeStr: ''
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 首页统计 硬盘 平均使用率 （新接口）
const getHomeDiskUsageAver = (type, isPermission , inCondition, equalCondition, timerl = timer) => {
    let data = {
        collection: type,
        equalCondition: {
            timeType: 1440,
            dataType: 'total',
            path: 'all',
            ...equalCondition
        },
        notEqualCondition: {
            instanceName: ""
        },
        rangeCondition: {
            beginTime: timerl,
        },
        orderField: {
            averUsedPercent: "desc"
        },
        returnFields: {
            averUsedPercent: 0
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 首页统计 CPU  内存  平均使用率 （新接口）
const getHomeUsageAver = (type, isPermission, fieldName = "averUsedPercent", inCondition, equalCondition, timerl = timer) => {
    let data = {
        collection: type,
        equalCondition: {
            timeType: 1440,
            ...equalCondition
        },
        notEqualCondition: {
            instanceName: ""
        },
        rangeCondition: {
            beginTime: timerl,
        },
        orderField: {
            [fieldName]: "desc"
        },
        returnFields: {
            [fieldName]: 0
        },
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// Cpu top 5
const getCpuTop5Usage = (timerl = timer, isPermission, inCondition, equalCondition) => {
    let data = {
        collection: 'cpu',
        equalCondition: {
            timeType: 1440,
            ...equalCondition
        },
        notEqualCondition: {
            instanceName: '',
        },
        rangeCondition: {
            beginTime: timerl,
        },
        groupFields: ['resourceUuid'],
        orderField: {
            averUsage: 'desc',
        },
        returnFields: {
            averUsage: 0,
            instanceName: ''
        },
        limit: 5,
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// mem top 5
const getMemTop5Usage = (timerl = timer, isPermission, inCondition, equalCondition) => {
    let data = {
        collection: 'mem',
        equalCondition: {
            timeType: 1440,
            ...equalCondition
        },
        notEqualCondition: {
            instanceName: '',
        },
        rangeCondition: {
            beginTime: timerl,
        },
        groupFields: ['resourceUuid'],
        orderField: {
            averUsedPercent: 'desc',
        },
        returnFields: {
            averUsedPercent: 0,
            instanceName: ''
        },
        limit: 5,
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }
    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// disk top 5
const getDiskTop5Usage = (timerl = timer, isPermission, inCondition, equalCondition) => {
    let data = {
        collection: 'disk',
        equalCondition: {
            timeType: 1440,
            dataType: 'total',
            path: 'all',
            ...equalCondition,
        },
        notEqualCondition: {
            instanceName: '',
        },
        rangeCondition: {
            beginTime: timerl,
        },
        groupFields: ['resourceUuid'],
        orderField: {
            averUsedPercent: 'desc',
        },
        returnFields: {
            averUsedPercent: 0,
            instanceName: ''
        },
        limit: 5,
    };
    if(isPermission) {
        data['permission'] = true;
    }
    if(inCondition) {
        data['inCondition'] = inCondition;
    }

    return {
        url: `/monitor/v1/oneAgg`,
        method: 'POST',
        data,
    };
};

// 分页查询告警通知记录
const queryAlarmNoticeLogs = {
    url: `${base}/alarm/v1/info/history`,
    method: 'GET',
    pro: ['data', 'data.list'],
};

// 确认告警信息
const confirmAlarmInfo = (alarmId) => ({
    url: `${base}/alarm/v1/info/confirm/${alarmId}`,
    method: 'PUT',
});
// 批量确认告警信息
const confirmAlarmInfoBatch = {
    url: `${base}/alarm/v1/info/confirm`,
    method: 'POST',
};
// 清除告警
const clearAlarmInfo = {
    url: `${base}/alarm/v1/info/clear`,
    method: 'PUT',
};
// 清除告警
const appointAlarmInfo = {
    url: `${base}/alarm/v1/info/alarmAppoint`,
    method: 'POST',
};
// 修改告警级别
const updateAlarmInfoLvl = {
    url: `${base}/alarm/v1/info/updateAlarmInfoLvl`,
    method: 'PUT',
};

/* --------------------- 告警规则 -------------------- */
// 获取资源类型
const queryCommonResource = {
    url: `${base}/alarm/v1/common/resource`,
    method: 'GET',
};
// 获取资源的指标列表
const queryCommonIndicator = (resourceCode) => ({
    url: `${base}/alarm/v1/common/${resourceCode}/indicator`,
    method: 'GET',
});
//  分页查询告警规则
const queryAlarmRule = {
    url: `${base}/alarm/v1/rule`,
    method: 'GET',
    pro: ['data', 'data.list'],
};
// 查询已设置告警规则的资源类型
const queryExistResourceType = {
    url: `${base}/alarm/v1/rule/existResourceType`,
    method: 'GET',
};
// 创建告警规则
const createAlarmRule = {
    url: `${base}/alarm/v1/rule`,
    method: 'POST',
};
// 根据id查询告警规则详情
const queryAlarmRuleInstance = (id) => ({
    url: `${base}/alarm/v1/rule/${id}`,
    method: 'GET',
});
// 根据id修改告警规则
const editAlarmRule = {
    url: `${base}/alarm/v1/rule`,
    method: 'PUT',
};
// 启用/禁用规则
const updateAlarmRuleStatus = (id, status) => ({
    url: `${base}/alarm/v1/rule/updateStatus/${id}/${status}`,
    method: 'PUT',
});
// 删除规则
const deleteAlarmRule = (id) => ({
    url: `${base}/alarm/v1/rule/${id}`,
    method: 'DELETE',
});
// 检查规则名称是否可用
const checkNameAlarmRule = {
    url: `${base}/alarm/v1/rule/name/check`,
    method: 'GET',
};
// 检查通知名称是否重复
const checkNameAlarmNoticeRule = {
    url: `${base}/alarm/v1/notice/name/check`,
    method: 'GET',
};
// 告警事件
const queryAlarmEventList = (resourceCode) => ({
    url: `${base}/alarm/v1/common/${resourceCode}/event`,
    method: 'GET',
});
// 检查指标是否已被设定
const checkAlarmRuleRepeat = {
    url: `${base}/alarm/v1/rule/repeat/check`,
    method: 'POST',
};
/* --------------------- 告警通知 -------------------- */
// 分页查询告警通知
const queryAlarmNotice = {
    url: `${base}/alarm/v1/notices`,
    method: 'GET',
};
// 创建告警通知
const createAlarmNotice = {
    url: `${base}/alarm/v1/notice`,
    method: 'POST',
};
// 修改告警通知
const editAlarmNotice = {
    url: `${base}/alarm/v1/notice`,
    method: 'PUT',
};
// 根据id查询告警通知详情
const queryAlarmNoticeInstance = (id) => ({
    url: `${base}/alarm/v1/notice/${id}`,
    method: 'GET',
});
// 启用/禁用通知
const updateAlarmNoticeStatus = (id) => ({
    url: `${base}/alarm/v1/notice/status/${id}`,
    method: 'POST',
});
// 删除通知
const deleteAlarmNotice = (id) => ({
    url: `${base}/alarm/v1/notice/${id}`,
    method: 'DELETE',
});
// 分页查询告警通知记录
const queryAlarmNoticeHistory = {
    url: `${base}/alarm/v1/notice/history`,
    method: 'GET',
};
// 告警通知铃声设置查询
const queryAlarmNoticeTone = {
    url: `${base}/alarm/v1/tone`,
    method: 'GET',
};
// 更新告警通知铃声设置
const updateAlarmNoticeTone = {
    url: `${base}/alarm/v1/tone/update`,
    method: 'PUT',
};
// 根据uuid查询实例ID
const getAlarmId  = (uuid) => {
    return {
        url: `${base}/alarm/v1/common/getInsId/${uuid}`,
        method: 'GET',
    }
}
// 查询指标组列表
const queryIndicatorGroups = {
    url: `${base}/alarm/v1/indicatorLibrary/indicator-groups`,
    method: 'GET',
};
// 查询指标列表
const queryIndicatorList = {
    url: `${base}/alarm/v1/indicatorLibrary/indicator-configs`,
    method: 'GET',
};
// 查询指标枚举值
const getIndicatorStatus = {
    url: `${base}/alarm/v1/indicatorLibrary/indicators`,
    method: 'GET',
};
// 告警实例组-查询
const getInsGroup = {
    url: `${base}/alarm/v1/insGroup`,
    method: 'GET',
};
// 告警实例组-创建
const createInsGroup = {
    url: `${base}/alarm/v1/insGroup`,
    method: 'POST',
};
// 告警实例组-删除
const deleteInsGroup = (insGroupId) => {
    return {
        url: `${base}/alarm/v1/insGroup/${insGroupId}`,
        method: 'DELETE',
    }
};
// 告警实例组-修改
const editInsGroup = {
    url: `${base}/alarm/v1/insGroup`,
    method: 'PUT',
};
// 告警实例组-检查实例组名称是否重复
const insGroupCheckName = {
    url: `${base}/alarm/v1/insGroup/name/check`,
    method: 'GET',
};
// 告警实例组-查询详情
const getInsGroupDetail = (insGroupId) => {
    return {
        url: `${base}/alarm/v1/insGroup/${insGroupId}`,
        method: 'GET',
    }
};
// 告警实例组-实例
const getInsGroupResources = {
    url: `${base}/alarm/v1/insGroup/getResources`,
    method: 'POST',
};
// 告警实例组-实例移除添加
const alarmOperationIns = {
    url: `${base}/alarm/v1/insGroup/operationIns`,
    method: 'PUT',
};


// 保存监控自定义设置
const updateIndicatorConfig = {
    url: `${base}/alarm/v1/indicatorLibrary/user-custom`,
    method: 'POST',
};
// 查询监控自定义设置
const queryIndicatorConfig = {
    url: `${base}/alarm/v1/indicatorLibrary/user-custom`,
    method: 'GET',
};
const hcpalarm = {
    queryCommonResource,
    queryCommonIndicator,
    queryAlarmRule,
    queryExistResourceType,
    createAlarmRule,
    queryAlarmRuleInstance,
    editAlarmRule,
    updateAlarmRuleStatus,
    deleteAlarmRule,
    checkNameAlarmRule,
    queryAlarmEventList,
    checkNameAlarmNoticeRule,
    checkAlarmRuleRepeat,
    queryAlarmNotice,
    createAlarmNotice,
    editAlarmNotice,
    queryAlarmNoticeInstance,
    updateAlarmNoticeStatus,
    deleteAlarmNotice,
    queryAlarmNoticeHistory,
    queryAlarmNoticeTone,
    updateAlarmNoticeTone,
    queryAlarmInfo,
    queryAlarmInfoExt,
    queryAlarmNoticeLogs,
    confirmAlarmInfo,
    confirmAlarmInfoBatch,
    clearAlarmInfo,
    appointAlarmInfo,
    updateAlarmInfoLvl,
    queryAlarmInfoAgg,
    queryNearly7DaysAlarm,
    queryScreenPageEchars,
    realtimeAlarmNum,
    getAlarmLvlNum,
    getNearly7DaysAlarmGroupBy,
    getRealtimeAlarmGroupBy,
    getRealtimeAlarmClass,
    getEcharsData,
    getNetEcharsData,
    getNetTop5Usage,
    getHomeDiskUsageAver,
    getHomeUsageAver,
    getCpuEcharsData,
    getCpuTop5Usage,
    getMemEcharsData,
    getDiskEcharsData,
    getMemTop5Usage,
    getDiskTop5Usage,
    getAlarmId,
    getIndicatorStatus,
    getInsGroup,
    createInsGroup,
    deleteInsGroup,
    editInsGroup,
    insGroupCheckName,
    getInsGroupDetail,
    getInsGroupResources,
    alarmOperationIns,
    updateIndicatorConfig,
    queryIndicatorConfig,
    queryIndicatorGroups,
    queryIndicatorList
};

export default hcpalarm;
